﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebLisans
{
    public partial class Query : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private string Kontrol(HttpRequest context)
        {
            if (context.QueryString.Count > 3)
            {
                string pcadi = context.QueryString["pc"];
                string dbadi = context.QueryString["db"];
                string srv = context.QueryString["srv"];
                string tip = context.QueryString["tip"];

                byte status = 254;
                string mesaj = "";

                using (SqlConnection cn = new SqlConnection("Data Source=mssql03.natro.com;Initial Catalog=DB121031105745;Persist Security Info=True;User ID=USR121031105745;Password=PSS!B6T1Z5%"))
                {
                    cn.Open();
                    using (SqlCommand cmd = new SqlCommand("select Status,ErrorMessage from LicensePool where MachineName=@pc and DatabaseName=@db and ServerName=@srv and ProgramType=@tip", cn))
                    {
                        cmd.Parameters.Add("@pc", System.Data.SqlDbType.VarChar, 50).Value = pcadi;
                        cmd.Parameters.Add("@db", System.Data.SqlDbType.VarChar, 50).Value = dbadi;
                        cmd.Parameters.Add("@srv", System.Data.SqlDbType.VarChar, 50).Value = srv;
                        cmd.Parameters.Add("@tip", System.Data.SqlDbType.TinyInt).Value = byte.Parse(tip);
                        SqlDataReader rd = cmd.ExecuteReader();
                        if (rd.HasRows)
                        {
                            rd.Read();
                            status = rd.GetByte(0);
                            mesaj = rd.GetString(1);
                            rd.Close();
                        }
                        else
                        {
                            status = 4;
                        }
                    }
                    cn.Close();
                }

                switch (status)
                {
                    case 0: //onay bekliyor
                        return "0";
                    case 1: //lisanslı program
                        return "1";
                    case 2: //lisansı kapandı
                        return "2";
                    case 3: // lisansı kapandı mesajla bildir.
                        return mesaj;
                    case 4: //yeni kayıt
                        Kaydet(pcadi, dbadi, srv, byte.Parse(tip));
                        break;
                }
            }
            return "0";
        }

        //void MailGonder(string pcadi, string dbadi, string srv, byte tip, string ekmesaj)
        //{
        //    try
        //    {
        //        System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com", 465);
        //        smtp.EnableSsl = true;
        //        smtp.UseDefaultCredentials = false;
        //        smtp.Credentials = new System.Net.NetworkCredential("tr.freelancer@gmail.com", "kahraman");
        //        smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
        //        smtp.Send(new System.Net.Mail.MailMessage("tr.freelancer@gmail.com", "yasin.ozbey@gmail.com", "Mikro Mağaza", "Sunucu :" + srv + "  Db:" + dbadi + " Pc Adı:" + pcadi + "  Program Tipi :" + tip + "          mesaj:" + ekmesaj));
        //    }
        //    catch (Exception)
        //    {

        //    }
        //}

        private void Kaydet(string pcadi, string dbadi, string srv, byte tip)
        {
            using (SqlConnection cn = new SqlConnection("Data Source=mssql03.natro.com;Initial Catalog=DB121031105745;Persist Security Info=True;User ID=USR121031105745;Password=PSS!B6T1Z5%"))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand("insert into LicensePool (Status,ErrorMessage,MachineName,DatabaseName,ServerName,ProgramType)  values (0,'',@pc,@db,@srv,@tip)", cn))
                {
                    cmd.Parameters.Add("@pc", System.Data.SqlDbType.VarChar, 50).Value = pcadi;
                    cmd.Parameters.Add("@db", System.Data.SqlDbType.VarChar, 50).Value = dbadi;
                    cmd.Parameters.Add("@srv", System.Data.SqlDbType.VarChar, 50).Value = srv;
                    cmd.Parameters.Add("@tip", System.Data.SqlDbType.TinyInt).Value = tip;
                    cmd.ExecuteNonQuery();
                }
                cn.Close();
            }
        }

        public override void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write(Kontrol(context.Request));
        }
    }
}